﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Text;
using System.Xml;
using System.Resources;
using System.IO;
using System.Reflection;
using Tesla.Service.DataContracts;

namespace Tesla.Util.Utilities
{
    public static class GenericConversion
    {
        public static IList<BaseDC> ConvertToTargetType(object[] sources, Type sourceType, Type targetType)
        {
            PropertyInfo[] sourceProperties = sourceType.GetProperties(),
                     targetProperties = targetType.GetProperties();
            List<BaseDC> targets = new List<BaseDC>();

            foreach (object source in sources)
            {
                object target = Activator.CreateInstance(targetType);
                foreach (PropertyInfo propSrc in sourceProperties)
                {
                    foreach (PropertyInfo propTgt in targetProperties)
                    {
                        if (string.Compare(propSrc.Name.Trim().ToUpper(), propTgt.Name.Trim().ToUpper()) == 0)
                            propTgt.SetValue(target, propSrc.GetValue(source, null), null);
                    }
                }
                targets.Add((BaseDC)target);                
            }
            return targets;
        }

        
    }
}
